<?
include('wallet_db.html');
$params[sid] = $sid;
$params[action] = "planStats";
$url = "$baseURL/?".encode_params($params);

$clients_th = translate('clients');
$details_th = translate('details');
$product_id_th = translate('product_id');
$product_name_th = translate('product_name');
$view_link = translate('view_link');
$message1 = translate('message1');
$message2 = translate('message2');

//------------------------------------------------------
$sql['type']='SELECT';
$sql['params']='product_id, name';
$sql['table']='products';
$sql['where']="reseller_id='$id' AND type='plan'";
$sql['options']='order by seq asc';
$sql['return']='struct';
//------------------------------------------------------
if (!$result=sql_exec($sql,$dbh81,'assoc')) {
	exit("<br><div class=errormsg>$dberror</div><br>");
}

echo "<script language='JavaScript'>
<!--
var myWin = '';


function openWin(url, name, properties) {
    if (myWin && myWin.open && !myWin.closed) {
	myWin.close();
    }
    myWin = window.open(url, name, properties);
    return false;
}
function confirm_del(what) {
        var is_confirmed=window.confirm('Are you sure you want to delete '+what+' plan?');
        return is_confirmed;
}
//-->
</script>";
echo "<div align='left'>".$message1;



if (isset($sub)) {
        include('actionOnPlans.html');
} elseif ($hide_rest != 'yes') {
    echo $message2;
    //------------------------------------------------------
	    $sql['type']='SELECT';
	    $sql['params']='count(product_id) as count';
	    $sql['table']='products';
	    $sql['where']="reseller_id='$id' and type='plan'";
	    $sql['options']='';
	    $sql['return']='struct';
    //------------------------------------------------------
    if (!$relt=sql_exec($sql,$dbh81,'assoc')) {
	   exit("<br><div class=errormsg>$dberror 1</div><br>");
    }
    $count=$relt[0][count];
    if ($count>=20) {
	    $nomoreplans=translate('nomoreplans',array('count' => "$count"));
	    echo "<br><div align='left'>$nomoreplans</div><br>";
    } else {
	    $params[sid] = $sid;
	    $params[action] = "createPlan";
	    $createurl = "$baseURL/?".encode_params($params);
	    unset($params);
	    $new_count = 20 - $count;
	    $moreplans=translate('moreplans',array('count' => "$count",'new_count' => "$new_count",'url' => "$createurl"));
	    echo "<br><div align='left'>$moreplans</div><br>";
    }

    echo "<br><TABLE class='bTable' cellspacing=0>
    <tr>
	    <th>$product_id_th</th>
	    <th>$product_name_th</th>
	    <th>$details_th</th>
	    <th>$clients_th</th>
	    <th>-</th>
	    <th>-</th>
    </tr>";
    foreach ($result as $value) {

	    //------------------------------------------------------
	    $sql['type']='SELECT';
	    $sql['params']='count(distinct pu.client_id) as count';
	    $sql['table']='purchases as pu, products as pr, clients as c';
	    $sql['where']="pr.product_id='".$value['product_id']."' and pr.product_id=pu.product_id and pu.client_id=c.client_id and c.blocked=0";
	    $sql['options']='group by pu.product_id';
	    $sql['return']='struct';
	    //------------------------------------------------------
	    if (!$result=sql_exec($sql,$dbh81,'assoc')) {
		    $count = '0';
	    } else {
		    $count = $result[0]['count'];
	    }
	    
	    $params[sid] = $sid;
	    $params[action] = "showPlanDetails";
	    $params[rid] = $id;
	    $params[pid] = $value['product_id'];
	    $detailsurl = "$baseURL/?".encode_params($params);
	    unset($params);
	    $params[sid] = $sid;
	    $params[action] = "showPlanClientsDetails";
	    $params[rid] = $id;
	    $params[pid] = $value['product_id'];
	    $detailsurl2 = "$baseURL/?".encode_params($params);
	    unset($params);



	    $params[sid] = $sid;
	    $params[action] = "managePlans";
	    $params[prod_id] = $value['product_id'];
	    $params[sub] = 'edit';
	    $params[cl_count] = $count;
	    $editurl = "$baseURL/?".encode_params($params);
	    unset($params);
	    $params[sid] = $sid;
	    $params[action] = "managePlans";
	    $params[prod_id] = $value['product_id'];
	    $params[sub] = 'del';
	    $params[cl_count] = $count;
	    $deleteurl = "$baseURL/?".encode_params($params);
	    unset($params);
	    if ($count != 0 && !in_array($value['name'],array('Business','Corporate'))) {
		    $ehref="<a href=\"$editurl\">edit</a>";
		    $dhref="<a href=\"$deleteurl\">delete</a>";
	    } elseif (in_array($value['name'],array('Business','Corporate'))) {
		    $ehref= '-';
		    $dhref= '-';
	    } else {
		    $ehref="<a href=\"$editurl\">edit</a>";
		    $dhref="<a href=\"$deleteurl\" onClick=\"return confirm_del('$value[name]');\">delete</a>";
	    }

	    echo "<tr>
	    <td>".$value['product_id']."</td>
	    <td>".$value['name']."</td>
	    <td><a href='#' onClick=\"return openWin('$detailsurl','Plan_Details','width=650, height=650, scrollbars=yes');\">$view_link</a></td>";
	    if ($count != 0) {
		    echo "<td><a href='#' onClick=\"return openWin('$detailsurl2','Plan_Details','width=850, height=450, scrollbars=yes');\">$count</a></td>";
	    } else {
		    echo "<td>$count</td>";
	    }
	    echo "<td>$ehref</td>
            <td>$dhref</td>";

	    echo "</tr>";
    }
    echo "</table>";

    unset($params);
    $params['sid'] = $sid;
    $params['action'] = "comparePlans";
    $params['from_page'] = 'planStats';
    $compareurl = "$baseURL/?".encode_params($params);
    echo "<br><a href='#' onClick=\"return openWin('$compareurl','Compare_Plans','width=650, height=550, scrollbars=yes');\">Compare Plans</a></div>";
}
?>
